import cgi

from google.appengine.api import users
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db

class Cities(db.Model):
  city = db.StringProperty()
  state = db.StringProperty()
  country = db.StringProperty()
  tz = db.StringProperty()
  lat = db.StringProperty()
  long = db.StringProperty()


class Search(webapp.RequestHandler):
  def get(self):
    self.response.headers['Content-Type'] = 'text/html'
    query = self.request.get('q', '')
    tmp = self.request.get('limit', '')
    self.response.out.write('query: %s, limit: %s<br>\n' % (query, tmp))

    limit = int(tmp)
    self.response.out.write('query: %s, limit: %d<br>' % (query, limit))
    cities = db.GqlQuery("SELECT * FROM Cities WHERE country >= 'So' AND country < 'Sp' LIMIT 50")

    for city in cities:
      self.response.out.write('City: %s, State: %s, Country: %s<br>' % (city.city, city.state, city.country))


application = webapp.WSGIApplication(
                                     [('/search', Search)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()
